#include "voice_lib.h"
void CallVoidMethod(  JNIEnv* env,const char *clzS,const char *method,const char *methodSig,jstring p){
    jclass clz = env->FindClass(clzS);
    jobject  obj = env->AllocObject(clz);
    jmethodID jmethodId = env->GetMethodID(clz,method,methodSig);
    env->CallVoidMethod(obj,jmethodId,p);
    env->DeleteLocalRef(obj);
    env->DeleteLocalRef(clz);
}

void * run(void * mypara) {
    JNIEnv *env;
    if(g_jvm->AttachCurrentThread(&env, NULL) != JNI_OK){
        return NULL;
    }
    Mypara mypara1 = *static_cast<Mypara *>(mypara);
    jint mode_normal = mypara1.para1;
    const char * path_ = mypara1.path_;
    System *system = 0;
    Sound * sound = 0;
    Channel * channel = 0;
    DSP * dsp = 0;
    System_Create(&system);
    system->init(32,FMOD_INIT_NORMAL,0);
    system->createSound(path_,FMOD_DEFAULT,00,&sound);
    system->playSound(sound,0, false,&channel);
    switch (mode_normal) {
        case MODE_NORMAL:
            break;
        case MODE_LUOLI:
            system->createDSPByType(FMOD_DSP_TYPE_PITCHSHIFT,&dsp);
            dsp->setParameterFloat(FMOD_DSP_PITCHSHIFT_PITCH,2.0f);
            channel->addDSP(0,dsp);
            break;
        case MODE_DASHU:
            system->createDSPByType(FMOD_DSP_TYPE_PITCHSHIFT,&dsp);
            dsp->setParameterFloat(FMOD_DSP_PITCHSHIFT_PITCH,0.7f);
            channel->addDSP(0,dsp);
            break;
        case MODE_JINGSONG:
            //音调低
            system->createDSPByType(FMOD_DSP_TYPE_PITCHSHIFT,&dsp);
            dsp->setParameterFloat(FMOD_DSP_PITCHSHIFT_PITCH,0.7f);
            channel->addDSP(0,dsp);
            //回音
            system->createDSPByType(FMOD_DSP_TYPE_ECHO,&dsp);
            dsp->setParameterFloat(FMOD_DSP_ECHO_DELAY,200);
            dsp->setParameterFloat(FMOD_DSP_ECHO_FEEDBACK,10);
            channel->addDSP(1,dsp);
            //颤抖
            system->createDSPByType(FMOD_DSP_TYPE_TREMOLO,&dsp);
            dsp->setParameterFloat(FMOD_DSP_TREMOLO_FREQUENCY,15.0f);
            dsp->setParameterFloat(FMOD_DSP_TREMOLO_SKEW,0.8f);
            channel->addDSP(2,dsp);
            break;
        case MODE_KONGLING:
            //回音
            system->createDSPByType(FMOD_DSP_TYPE_ECHO,&dsp);
            dsp->setParameterFloat(FMOD_DSP_ECHO_DELAY,200);
            dsp->setParameterFloat(FMOD_DSP_ECHO_FEEDBACK,10);
            channel->addDSP(0,dsp);
            break;
        case MODE_GAOGUAI:
            //从音轨那频率
            float  mFrequemcy;
            channel->getFrequency(&mFrequemcy);
            //修改频率
            channel->setFrequency(mFrequemcy * 1.5f);
            break;

    }
    bool isPaly = true;
    LOGD("isPlaying=%d",isPlaying);
    while (isPaly){
        channel->isPlaying(&isPaly);
        usleep(1000 * 1000);
    }
    LOGD("isPlaying=%d",isPlaying);
    isPlaying = false;
    LOGD("播放完了isPlaying=%d",isPlaying);
    jclass jclass1 = env->GetObjectClass(g_obj);
    jmethodID jmethodId = env->GetMethodID(jclass1,"onFinish","()V");
    env->CallVoidMethod(g_obj,jmethodId);
    sound->release();
    system->close();
    system->release();
    env->DeleteGlobalRef(g_obj);
    env->DeleteLocalRef(jclass1);
    jclass1 = nullptr;
   // env->DeleteLocalRef(mypara1);

    return 0;
}
extern "C"
JNIEXPORT void JNICALL
Java_org_lib093_ndkapplication_VoiceManager_voiceChangeNative(JNIEnv *env, jobject thiz,
                                                              jint mode_normal, jstring path) {

    env->GetJavaVM(&g_jvm);
//不能直接赋值(g_obj = obj)
   g_obj = env->NewGlobalRef(thiz);
    if (isPlaying){
        CallVoidMethod(env,"org/lib093/ndkapplication/VoiceManager","show","(Ljava/lang/String;)V",env->NewStringUTF("当前正在播放请稍后...."));
        return;
    }
    jclass jclass1 = env->GetObjectClass(thiz);
    jmethodID jmethodId = env->GetMethodID(jclass1,"onStart","()V");
    env->CallVoidMethod(thiz,jmethodId);
   isPlaying = true;
    mypara.para1 = mode_normal;
    mypara.path_ = env->GetStringUTFChars(path, nullptr);
    mypara.thiz = env->NewGlobalRef(thiz);
    pthread_create(&pid, 0, run, &mypara);
//    pthread_join(pid, 0);
}
//static const JNINativeMethod jniNativeMethod[] ={
//        {stringFromJNIName,"()Ljava/lang/String;",(jstring*)(stringFromJNI)}
//};
/////动态注册 相当于构造方法
//JNIEXPORT jint JNI_OnLoad(JavaVM * javaVm,void *){
//    ::g_jvm = javaVm;
//    //做动态注册
//    JNIEnv *jniEnv;
//    int r = g_jvm->GetEnv(reinterpret_cast<void **>(&jniEnv), JNI_VERSION_1_6);
//    if (r != JNI_OK){
//        return -1;
//    }
//    LOGD(TAG,"JNIEnv获取成功");
//    jclass  clz = jniEnv->FindClass(clzName);
//
//    jniEnv->RegisterNatives(clz,jniNativeMethod, sizeof(jniNativeMethod)/ sizeof(JNINativeInterface));
//    return JNI_VERSION_1_6;
//}

jstring stringFromJNI(
        JNIEnv* env,
        jobject /* this */) {
    std::string hello = "Hello from C++";
    CallVoidMethod(env,"org/lib093/ndkapplication/Sturent","show","(Ljava/lang/String;)V",env->NewStringUTF("我曹"));
    return env->NewStringUTF(hello.c_str());
}

extern "C"
JNIEXPORT jboolean JNICALL
Java_org_lib093_ndkapplication_VoiceManager_getPlayStateNative(JNIEnv *env, jobject thiz) {
    return isPlaying;
}